package com.dmz.workflow.controller;

import com.dmz.workflow.req.ListQuery;
import com.dmz.workflow.vo.IntermediateNodeVo;
import com.dmz.workflow.vo.TaskListVo;
import com.echronos.commons.Result;
import com.echronos.commons.utils.RequestUserUtils;
import com.dmz.workflow.service.FlowableTaskService;
import com.dmz.workflow.service.PermissionService;
import com.echronos.workflow.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 节点操作
 **/
@Slf4j
@RestController
@RequestMapping("/v1/flowable/task")
public class TaskOperationController {
    @Autowired
    protected FlowableTaskService flowableTaskService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private FormService formService;

    /**
     * 审核通过  提交按钮
     * 完成任务
     */
    @PutMapping(value = "/complete")
    public Result<?> complete(@RequestBody @Validated CompleteReq completeReq) {
        return flowableTaskService.completeTask(completeReq);
    }

    /**
     * 退回任务
     */
    @PostMapping(value = "/back")
    public Result<?> back(@RequestBody @Validated BackReq backReq) {
        flowableTaskService.backTask(backReq);
        return Result.build();
    }

    /**
     * 审批拒绝
     */
    @PutMapping(value = "/refuse")
    public Result<?> refuse(@RequestBody CompleteReq completeReq) {
        return flowableTaskService.refuseTask(completeReq);
    }

    /**
     * 审批撤销
     */
    @PutMapping(value = "/repeal")
    public Result<?> repeal(@RequestBody CompleteReq completeReq) {
        flowableTaskService.repealTask(completeReq);
        return Result.build();
    }

    /**
     * 委派任务
     * 委派：是将任务节点分给其他人处理，等其他人处理好之后，委派任务会自动回到委派人的任务中
     */
    @PostMapping(value = "/delegate")
    public Result<?> delegate(@RequestBody @Validated DelegateReq delegateReq) {
        return flowableTaskService.delegateTask(delegateReq);
    }

    /**
     * 转办/转交任务
     */
    @PostMapping(value = "/assign")
    public Result<?> assign(@RequestBody @Validated DelegateReq delegateReq) {
        return flowableTaskService.assignTask(delegateReq);
    }

    /**
     * 我的已办列表接口
     * 根据发起人名称 流程名称 查找
     **/
    @GetMapping(value = "/listDone")
    public Result<List<TaskListVo>> listDone(ListQuery queryVo) {
        return flowableTaskService.getTaskDone(queryVo);
    }

    /**
     * 审批中任务列表接口
     * 根据发起人名称 流程名称 查找
     */
    @GetMapping(value = "/listTodo")
    public Result<List<TaskListVo>> listTodo(ListQuery queryVo) {
        return flowableTaskService.getToDoList(queryVo);
    }

    /**
     * 统计待我审批的数量
     */
    @GetMapping(value = "/countListTodo")
    public Result<Integer> countListTodo() {
        return Result.build(flowableTaskService.countToDoList());
    }

    /**
     * 查询可退回的流程节点
     */
    @GetMapping(value = "/queryReachableBackNodes")
    public Result<List<IntermediateNodeVo>> queryReachableBackNodes(@RequestParam String processInstanceId) {
        return Result.build(flowableTaskService.queryReachableBackNodes(processInstanceId));
    }

    /**
     * 阅读任务表单
     */
    @GetMapping(value = "/renderedTaskForm")
    public Result<Object> renderedTaskForm(@RequestParam String taskId) {
        String userId = String.valueOf(RequestUserUtils.getUser().getMemberId());
        permissionService.validateReadPermissionOnTask2(taskId, userId, true, true);
        return Result.build(formService.getRenderedTaskForm(taskId));
    }

    /**
     * 批量读取抄送的审批列表
     */
    @PutMapping(value = "/read")
    public Result<?> read(@RequestBody ReadReq readReq) {
        flowableTaskService.readTask(readReq);
        return Result.build();
    }

    /**
     * 标记个人所有抄送为已读状态
     */
    @PostMapping(value = "/markAllRead")
    public Result<?> markAllRead() {
        flowableTaskService.markAllRead();
        return Result.build();
    }

    /**
     * 催办任务
     */
    @PostMapping(value = "/urgeToApprove")
    public Result<?> urgeToApprove(@RequestBody @Validated UrgeToApproveReq urgeToApprove) {
        flowableTaskService.urgeToApprove(urgeToApprove);
        return Result.build();
    }

    /**
     * 向前、向后加签
     */
    @PostMapping(value = "/addSignTask")
    public Result<?> addSignTask(@RequestBody AddSignReq params) {
        if (params.getSignPersons() == null || params.getSignPersons().size() <= 0) {
            return Result.build("请选择人员");
        }
        flowableTaskService.addSignTask(params);
        return Result.build();
    }

    /**
     * 更新任务表单的数据
     */
    @PostMapping(value = "/updateTaskFormData")
    public Result<?> updateTaskFormData(@RequestBody TaskFormDataReq taskFormDataReq) {
        flowableTaskService.updateTaskFormData(taskFormDataReq);
        return Result.build();
    }
}
